In this step of the tutorial you create keyboard navigation between the Home, Media, Car, and application screens. You use triggers with JavaScript scripts to set which node receives the key input when the user presses a key on their keyboard.
The starting point of this tutorial is the Keyboard input.kzproj Kanzi Studio project file stored in <KanziWorkspace>/Tutorials/Keyboard input/Start directory.
The <KanziWorkspace>/Tutorials/Keyboard input/Completed directory contains the completed project of this tutorial.
The starting point project contains the content you need to complete this tutorial:
The OpenMap node which users use to navigate to the Map application screen.
The OpenMap node which users use to navigate to the Map application screen.
In this section you create keyboard navigation for the toggle buttons you use to navigate between the Page nodes which show the application screens.
To create keyboard navigation between application screens:
In the Node Components click Trigger Settings and in the Trigger Settings Editor click Add condition. The Trigger Condition Editor opens.
Trigger conditions enable you to set which conditions must be met for the trigger to set off. Here you set the Key Down trigger to be set off when the user presses a specific key on the keyboard. You set which key sets off the trigger in the next step of this section.
In the Trigger Condition Editor and the Trigger Settings Editor windows click Save.
With this condition when the user presses down the → (Right Arrow) key, the Key Down trigger executes the actions you set in the trigger. For a list of the keyboard key codes used in Kanzi, see Keyboard input codes reference.
// Get the Page node Home. var homePage = node.lookupNode('#Home'); // The value of the Page.State property tells whether the Page or Page Host node is active: // - If the value is 0, the node is inactive and invisible. // - If the value is 1, the node is active and visible. var homeActive = homePage.getProperty('Page.State'); var mediaPage = node.lookupNode('#Media'); var mediaButton = node.lookupNode('#MediaButton'); var mediaActive = mediaPage.getProperty('Page.State'); var carPage = node.lookupNode('#Car'); var carButton = node.lookupNode('#CarButton'); var carActive = carPage.getProperty('Page.State'); var navigationPage = node.lookupNode('#Navigation'); var navigationButton = node.lookupNode('#NavigationButton'); var navigationActive = navigationPage.getProperty('Page.State'); var screen = node.lookupNode('#Screen'); // Check which application Page node is active and navigate to the next application. if (homeActive == 1) { // Navigate to the Media application screen. mediaPage.navigateToThisPage(); // Set the Toggle State for the button of the next application to 1. // Toggle State Controller Property controls the state of these toggle buttons. mediaButton.setProperty('ButtonConcept.ToggleState', 1); } else if (mediaActive == 1) { carPage.navigateToThisPage(); carButton.setProperty('ButtonConcept.ToggleState', 1); } else if (carActive == 1) { navigationPage.navigateToThisPage(); navigationButton.setProperty('ButtonConcept.ToggleState', 1); }
You use the script to set which Toggle Button node receives the keyboard input.
To try this out in the Kanzi Studio Preview, click the Preview window to set the focus to the Preview window and press the → (Right Arrow) key on your keyboard to navigate between the Home, Media, Car, and application screens. When Kanzi activates each of the Page nodes, it sets the value of the Toggle State property of the Toggle Button which navigates to that Page to 1.
In the Node Components click Trigger Settings and in the Trigger Settings Editor click Add condition.
In the Trigger Condition Editor and the Trigger Settings Editor windows click Save.
With this condition when the user presses down the ← (Left Arrow) key, the Key Down trigger executes the actions you set in the trigger.
In the Node Components add to the Key Down trigger the Execute Script action.
You set the application to execute a JavaScript script when the user presses the ← (Left Arrow) key.
// Get the Page node Home. var homePage = node.lookupNode('#Home'); // Get the > Grid Layout 2D > HomeButton node. var homeButton = node.lookupNode('#HomeButton'); // The value of the Page.State property tells whether the Page or Page Host node is active: // - If the value is 0, the node is inactive and invisible. // - If the value is 1, the node is active and visible. var homeActive = homePage.getProperty('Page.State'); var mediaPage = node.lookupNode('#Media'); var mediaButton = node.lookupNode('#MediaButton'); var mediaActive = mediaPage.getProperty('Page.State'); var carPage = node.lookupNode('#Car'); var carButton = node.lookupNode('#CarButton'); var carActive = carPage.getProperty('Page.State'); var navigationPage = node.lookupNode('#Navigation'); var navigationActive = navigationPage.getProperty('Page.State'); var mapPage = node.lookupNode('#Map'); var mapActive = mapPage.getProperty('Page.State'); // Check which application Page node is active and navigate to the next application. if (navigationActive == 1 && mapActive != 1) { carPage.navigateToThisPage(); // Set the Toggle State for the button of the next application to 1. // Toggle State Controller Property controls the state of these toggle buttons. carButton.setProperty('ButtonConcept.ToggleState', 1); } else if (carActive == 1) { mediaPage.navigateToThisPage(); mediaButton.setProperty('ButtonConcept.ToggleState', 1); } else if (mediaActive == 1) { homePage.navigateToThisPage(); homeButton.setProperty('ButtonConcept.ToggleState', 1); }
You can now use the → (Right Arrow) and ← (Left Arrow) keyboard keys to navigate between application screens.
In this section you rename the scripts you created in this step of the tutorial and create folders for those scripts.
To organize the script files in your Kanzi Studio project:
Tutorial: Create application flow with Page nodes
Using the Page and Page Host nodes